

import bpy

import 物体.物体管理.物体数据 as 网格数据

from 节点.全局控制.Node.节点和插座基类 import bl_节点基类
from 节点.Core import bl_UI配色
from 节点.Core import bl_Mesh

import engine.initLIB.load_dll as engineLib
import ctypes


engineLib.flexCAPI.f_创建文字曲线节点.restype = ctypes.c_bool




bpy.types.BlendDataFonts




class C_Node_字体(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_字体'
	bl_label = '字体'

	def init(self, context):
		super(C_Node_字体, self).f_init(context)












class C_曲线对象指针(bpy.types.PropertyGroup):
	m_object = bpy.props.PointerProperty(type=bpy.types.TextCurve)

def f_update_字体修改(self, context):
	filepath = self.m_字体
	t = bpy.data.fonts.load(filepath, check_existing=False)
	print('filepath = ', filepath, t)

class C_Node_文字曲线(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_文字曲线'
	bl_label = '文字曲线'
	
	m_Value = bpy.props.CollectionProperty(type=C_曲线对象指针)
	#m_字体路径 = bpy.props.StringProperty(subtype='DIR_PATH')#subtype='FILE_NAME',  options='SKIP_SAVE'
	#m_字体 = bpy.props.StringProperty(subtype='FILE_PATH', update=f_update_字体修改)#subtype='FILE_NAME',  options='SKIP_SAVE'
	m_Object = bpy.props.PointerProperty(type=bpy.types.Object)
	m_是否分割成三角形 = bpy.props.BoolProperty(default=False)
	m_应用矩阵 = bpy.props.BoolProperty(default=False)
	m_封包 = bpy.props.BoolProperty(default=True)
	m_是否分离 = bpy.props.BoolProperty(default=False)
	def init(self, context):
		super(C_Node_文字曲线, self).f_init(context)
		self.inputs.new('C_Socket_文本', name='文本')
		self.inputs.new('C_Socket_物体_Array1D', name='倒角物体')
		self.inputs.new('C_Socket_Bl字体', name='字体')

		self.outputs.new('C_Socket_网格_Array1D', name='网格')
		
		self.use_custom_color = True
		self.color = bl_UI配色.g_内部节点颜色
		self.f_toCPP();

	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_Value')
		layout.prop(self, 'm_是否分割成三角形')
		layout.prop(self, 'm_应用矩阵')
		layout.prop(self, 'm_是否分离')
		#layout.prop(self, 'm_字体')
		#text = context.curve
		#char = context.curve.edit_format
		#row = layout.split(factor=0.25)
		#row.label(text="Regular")
		#row.template_ID(text, "font", open="font.open", unlink="font.unlink")
	def update(self):
		if len(self.outputs) >= 1 and self.outputs['网格'].is_linked :
			pass
			
	def f_toCPP(self):
		是否更新 = engineLib.flexCAPI.f_创建文字曲线节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		if 是否更新 :
			self.inputs['文本'].f_update()
			self.inputs['字体'].f_update()

	def f_update(self):
		文本     = self.inputs['文本'].f_getData()
		if 文本 == "":
			return
		倒角物体 = self.inputs['倒角物体'].f_getData()
		
		

		
		
		
		#创建文字曲线对象
		if self.m_是否分离 :
			bl_节点基类.f_CollectionProperty数量update(self.m_Value, len(文本))
			index = 0
			for t in 文本:
				if self.m_Value[index].m_object == None:
					data = bpy.data.curves.new('mbt', 'FONT')
					self.m_Value[index].m_object = data
				self.m_Value[index].m_object.body = t
				#print('self.m_Value[index].m_object = ', self.m_Value[index].m_object, t)
				index += 1
		else:
			bl_节点基类.f_CollectionProperty数量update(self.m_Value, 1)

			if self.m_Value[0].m_object == None :
				data = bpy.data.curves.new('mbt', 'FONT')
				self.m_Value[0].m_object = data
			self.m_Value[0].m_object.body = 文本
		
		
		字体 = self.inputs['字体'].f_getData()
		字体独立 = len(字体) >= len(self.m_Value)

		if len(字体) :
			index = 0
			for c in self.m_Value :
				if 字体独立 :
					c.m_object.font = 字体[index]
				else :
					c.m_object.font = 字体[0]
				index += 1


		if self.m_Object == None and len(self.m_Value):
			self.m_Object = bpy.data.objects.new(self.name, self.m_Value[0].m_object)
			bpy.context.collection.objects.link(self.m_Object)
		
		#倒角
		if len(倒角物体) :
			if len(倒角物体) >= len(self.m_Value) :
				倒角物体独立 = True
			else :
				倒角物体独立 = False
			index = 0
			
			for obj in self.m_Value:
				if 倒角物体独立 :
					obj.m_object.bevel_object = 倒角物体[index]
					index += 1
				else:
					obj.m_object.bevel_object = 倒角物体[0]
		
		
		


		#构建网格
		meshes = []
		for obj in self.m_Value:
			#if obj.m_object == None : continue
			#oldMe = self.m_Object.data
			#oldMe.user_clear()
			self.m_Object.data = obj.m_object
			#print('self.m_Object.data = ', self.m_Object, obj.m_object)
			MESH = 网格数据.f_get物体网格数据(self.m_Object, bpy.context, 导出三角面=self.m_是否分割成三角形, 应用矩阵=self.m_应用矩阵)
			#data = 网格数据.f_get物体网格数据(self.m_Object, bpy.context, 导出三角面=self.m_是否分割成三角形, 应用矩阵=self.m_应用矩阵, 封包=self.m_封包)
			#data.insert(0, list(''))
			meshes.append( MESH )
		engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(meshes));
		




class C_NODE_MENU_文字曲线(bpy.types.Operator):
	bl_idname = 'node.global_node_string_curves'
	bl_label = '文字曲线'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_文字曲线', use_transform=True)
		return {'FINISHED'}







classes = (
	C_曲线对象指针,

	C_Node_文字曲线,
	C_NODE_MENU_文字曲线,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)










